package com.community.body.service.impl;

import java.util.List;

import com.community.body.domain.ZyCommunityInteraction;
import com.community.body.dto.InteractionDTO;
import com.community.body.mapper.ZyCommunityInteractionMapper;
import com.community.body.mapper.ZyFilesMapper;
import com.community.body.service.IZyCommunityInteractionService;
import com.community.body.vo.ZyCommunityInteractionVO;
import com.community.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 社区互动Service业务层处理
 *
 * @author community
 * @date 2024-04-16
 */
@Service
public class ZyCommunityInteractionServiceImpl implements IZyCommunityInteractionService {
    @Resource
    private ZyCommunityInteractionMapper zyCommunityInteractionMapper;
    @Autowired
    private ZyFilesMapper zyFilesMapper;
    @Resource
    private RedisTemplate<String, Long> redisTemplate;

    /**
     * 查询社区互动
     *
     * @param interactionId 社区互动主键
     * @return 社区互动
     */
    @Override
    public ZyCommunityInteraction selectZyCommunityInteractionByInteractionId(Long interactionId) {
        return zyCommunityInteractionMapper.selectZyCommunityInteractionByInteractionId(interactionId);
    }

    /**
     * 查询社区互动列表
     *
     * @param interactionDTO 社区互动
     * @return 社区互动
     */
    @Override
    public List<InteractionDTO> selectZyCommunityInteractionList(InteractionDTO interactionDTO) {
        if (interactionDTO.getUserName() != null && !interactionDTO.getUserName().isEmpty() && interactionDTO.getUserName().equals("admin")) {
            interactionDTO.setDeptId(0);
        }
        return zyCommunityInteractionMapper.selectZyCommunityInteractionList(interactionDTO);
    }

    /**
     * 热评
     *
     * @param interactionDTO 社区互动
     * @return 社区互动
     */
    @Override
    public List<InteractionDTO> selectHotZyCommunityInteractionList(InteractionDTO interactionDTO) {
        return zyCommunityInteractionMapper.selectHotZyCommunityInteractionList(interactionDTO);
    }

    /**
     * 新增社区互动
     *
     * @param zyCommunityInteraction 社区互动
     * @return 结果
     */
    @Override
    public int insertZyCommunityInteraction(ZyCommunityInteractionVO zyCommunityInteraction) {
        ValueOperations<String, Long> value = redisTemplate.opsForValue();
        ZyCommunityInteraction communityInteraction = new ZyCommunityInteraction();
        communityInteraction.setCommunityId(zyCommunityInteraction.getCommunityId());
        communityInteraction.setCreateTime(DateUtils.getNowDate());
        communityInteraction.setContent(zyCommunityInteraction.getContent());
        communityInteraction.setDelFlag(0L);
        communityInteraction.setUserId(zyCommunityInteraction.getOwnerId());
        int i = zyCommunityInteractionMapper.insertZyCommunityInteraction(communityInteraction);
        value.set("communityInteractionId", communityInteraction.getInteractionId());
        return i;
    }

    /**
     * 修改社区互动
     *
     * @param zyCommunityInteraction 社区互动
     * @return 结果
     */
    @Override
    public int updateZyCommunityInteraction(ZyCommunityInteraction zyCommunityInteraction) {
        Long interactionID = zyCommunityInteraction.getInteractionId();
        zyCommunityInteractionMapper.updateCommentDelFlag(interactionID);
        zyCommunityInteraction.setUpdateTime(DateUtils.getNowDate());
        zyCommunityInteraction.setDelFlag(1L);
        return zyCommunityInteractionMapper.updateZyCommunityInteraction(zyCommunityInteraction);
    }

    /**
     * ,
     * 批量删除社区互动
     *
     * @param interactionIds 需要删除的社区互动主键
     * @return 结果
     */
    @Override
    public int deleteZyCommunityInteractionByInteractionIds(Long[] interactionIds) {
        return zyCommunityInteractionMapper.deleteZyCommunityInteractionByInteractionIds(interactionIds);
    }

    /**
     * 删除社区互动信息
     *
     * @param interactionId 社区互动主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteZyCommunityInteractionByInteractionId(Long interactionId) {
        int i = zyFilesMapper.deleteZyFilesByParentId(interactionId);
        int o = zyCommunityInteractionMapper.deleteZyCommunityInteractionByInteractionId(interactionId);
        return i + o > 0 ? 1 : 0;
    }
}
